package com.beagle.njddapp.ui.activity.owner;

import android.content.Intent;
import android.graphics.Color;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.beagle.njddapp.R;
import com.beagle.njddapp.base.BaseMvpActivity;
import com.beagle.njddapp.bean.MessageBean;
import com.beagle.njddapp.ui.adapter.NjMessageAdapter;
import com.beagle.njddapp.ui.adapter.inter.RecyclerViewItemClickLisenter;
import com.beagle.njddapp.ui.presenter.MessagePresenter;
import com.beagle.njddapp.ui.view.MessageView;
import com.beagle.njddapp.utils.StatusBarUtil;
import com.blankj.utilcode.util.ToastUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @author hxx
 * @date 2021-03-02 22:31
 * 农机主 消息
 */
public class MessageActivity extends BaseMvpActivity<MessageView, MessagePresenter> implements MessageView, RecyclerViewItemClickLisenter<MessageBean> {


    @BindView(R.id.commom_title_tv)
    TextView tvTitle;
    @BindView(R.id.common_container)
    RelativeLayout container;
    @BindView(R.id.common_tv_right)
    TextView tvRight;
    @BindView(R.id.tv_message_one)
    TextView tvOne;
    @BindView(R.id.tv_message_two)
    TextView tvTwo;
    @BindView(R.id.view2)
    View view2;
    @BindView(R.id.view1)
    View view1;
    @BindView(R.id.message_recyclerview)
    RecyclerView recyclerView;
    private int color_blue,color_black,type=1;
    private List<MessageBean> normalMessageList;//普通消息
    private List<MessageBean> messageList;//审核消息
    private List<MessageBean> data;
    private NjMessageAdapter messageAdapter;

    @Override
    public int getLayoutId() {
        return R.layout.activity_message;
    }




    @Override
    public void init() {
        StatusBarUtil.immersive(this);
        StatusBarUtil.darkMode(this);
        StatusBarUtil.setPaddingSmart(this, container);
        container.setBackgroundColor(Color.WHITE);
        normalMessageList=new ArrayList<>();
        messageList=new ArrayList<>();
        data=new ArrayList<>();
        tvTitle.setText("我的消息");
        presenter.message(1000,1);
        color_blue=Color.parseColor("#0049FF");
        color_black=getResources().getColor(R.color.color_0D0D0D);
        messageAdapter=new NjMessageAdapter(data,this);
        messageAdapter.setOnItemClickLisenter(this);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setAdapter(messageAdapter);
    }

    @Override
    public MessageView getMvpView() {
        return this;
    }

    @Override
    public MessagePresenter createPresenter() {
        return new MessagePresenter();
    }

    @Override
    public void onError(String message) {
        ToastUtils.showShort(message);
    }



    @OnClick({R.id.tv_message_one,R.id.common_back_iv,R.id.tv_message_two})
    public void onMyClick(View view){
        switch (view.getId()){
            case R.id.common_back_iv:
                onBackPressed();
                break;
            case R.id.tv_message_two:
                if (type==1){
                    return;
                }
                type=1;
                tvTwo.setTextColor(color_blue);
                view2.setBackgroundColor(color_blue);
                tvOne.setTextColor(color_black);
                view1.setBackgroundColor(Color.WHITE);
                data.clear();
                data.addAll(messageList);
                messageAdapter.notifyDataSetChanged();
                break;
            case R.id.tv_message_one:
                if (type==2){
                    return;
                }
                type=2;
                tvOne.setTextColor(color_blue);
                view1.setBackgroundColor(color_blue);
                tvTwo.setTextColor(color_black);
                view2.setBackgroundColor(Color.WHITE);
                data.clear();
                data.addAll(normalMessageList);
                messageAdapter.notifyDataSetChanged();
                break;

        }
    }

    @Override
    public void showMessage(List<MessageBean> results) {
        normalMessageList.clear();
        messageList.clear();
        for (MessageBean messageBean:results){
            if ("common".equals(messageBean.getMessageType())){
                normalMessageList.add(messageBean);
            }else {
                messageList.add(messageBean);
            }
        }
        if (type==1){
            data.clear();
            data.addAll(messageList);
            messageAdapter.notifyDataSetChanged();
        }else {
            data.clear();
            data.addAll(normalMessageList);
            messageAdapter.notifyDataSetChanged();
        }
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        presenter.message(1000,1);

    }

    @Override
    public void onItemClickLisenter(MessageBean messageBean) {
        startActivityForResult(new Intent(this,MessageDetailActivity.class).putExtra("data",messageBean),304);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (data!=null){
            MessageBean messageBean= (MessageBean) data.getSerializableExtra("data");
            if(type==1){
                for (int i=0;i<messageList.size();i++){
                    if (messageList.get(i).getId()==messageBean.getId()){
                        messageList.set(i,messageBean);
                    }
                }
            }else if (type==2){
                for (int i=0;i<normalMessageList.size();i++){
                    if (normalMessageList.get(i).getId()==messageBean.getId()){
                        normalMessageList.set(i,messageBean);
                    }
                }
            }
            messageAdapter.notifyDataSetChanged();
        }
    }
}
